import pygame
import math
from config import *

class vert:
    moved = 0
    scaled = 0
    rotated = 0
    x = 0
    y = 0
    z = 0
    def __init__(self,x,y,z):
        self.x = x
        self.y = y
        self.z = z
    def move(self,x2,y2,z2):
        self.moved = 1
        self.x +=x2
        self.y +=y2
        self.z +=z2
    def scale(self,x,y,z):
        self.scaled = 1
        self.x *=x
        self.y *=y
        self.z *=z
    def rotate(self,x,y,z):
        self.rotated = 1
        #rotate x
        self.y = math.cos(math.radians(x))*self.y
        self.z = math.sin(math.radians(x))*self.z  
        #rotate y
        #self.x = math.cos(y)*self.x
        #self.y = math.sin(y)*self.y  
        #rotate z
        #self.x = math.cos(z)*self.x
        #self.y = math.sin(z)*self.y  
class face:
    face = []
    face2d = []
    def __init__(self,poly1,poly2,poly3):
        self.face.append(poly1)
        self.face.append(poly2)
        self.face.append(poly3)
    def draw(self,col,screen):
        for i in self.face:
            self.face2d.append(Poly3dTo2d(i))
        pygame.draw.polygon(screen,col,self.face2d,1)
    def move(self,x2,y2,z2): 
        for i in self.face:
            if i.moved != 1:
                i.move(x2,y2,z2)
    def scale(self,x,y,z):
        for i in self.face:
            if i.scaled != 1:
                i.scale(x,y,z)
    def rotate(self,x,y,z):
        for i in self.face:
            if i.rotated != 1:
                i.rotate(x,y,z)
    def setmod(self,pos,rot,scale):
        for i in self.face:
            i.moved = pos
            i.rotated = rot
            i.scaled = scale
class object:
    pos = [0,0,0]
    scale = [1.0,1.0,1.0]
    rot = [0,0,0]
    faces = []
    def __init__(self,faces):
        for i in faces:
            self.faces.append(i)
    def draw(self,col,screen):
        pos = self.pos
        scale = self.scale
        rot = self.rot
        for i in self.faces:
            i.scale(scale[0],scale[1],scale[2])
            i.rotate(rot[0],rot[1],rot[2])
            i.move(pos[0],pos[1],pos[2])
            i.draw(col,screen)
            i.scale(1/scale[0],1/scale[1],1/scale[2])
        for i in self.faces:
            i.setmod(0,0,0)
    def setPos(self,pos):
        self.pos = pos
    def setScale(self,scale):
        self.scale = scale
    def setRotation(self,rot):

        self.rot = rot
            
            
def Poly3dTo2d(poly): 
    x,y,z = poly.x,poly.y,poly.z 
    x,y,z = float(x),float(y),float(z) 
    if z==0.0: 
        z=-0.1 
    #print fokus,z 
    sf = fokus/z 
    x = x*sf 
    y = y*sf 

    return PosFromMiddle(x,y)

def PosFromMiddle(x,y):
    #x *=-1
    y *=-1
    return (mx+x,my+y)